from django.db import models

class User(models.Model):
	ACCESS_CHOICES = (
	('RO','Read Only'),
	('QA','QA'),
	('Dev','Developer'),
	('Man','Manager'),
	('Adm','Admin'),
	)
	name = models.CharField(max_length=30)
	access = models.CharField(max_length=3, choices=ACCESS_CHOICES)
	password = models.TextField()
	
	def __unicode__(self):
		return self.name
	
	
class Project(models.Model):
	STATUS_CHOICES = (
		('Act','Active'),
		('Pen','Pending'),
		('Spn','Suspended'),
		('Cpl','Completed'),
		('Stp','Stopped'),
	)
	
	PRIOR_CHOICES = (
		('Low','Low'),
		('Nml','Normal'),
		('Hig','High'),
	)
	
	name = models.CharField(max_length=100)
	customer = models.TextField()
	manager = models.ForeignKey('User')
	status = models.CharField(max_length=3, choices=STATUS_CHOICES)
	priority = models.CharField(max_length=3, choices=PRIOR_CHOICES)

	def __unicode__(self):
		return self.name
	

class Bug(models.Model):
	KIND_CHOICES = (
		('IF','Incorrect Function'),
		('FC','Fatal Crash'),
		('IP','Installation problem'),
		('UI','UI Problem'),
		('TE','Textual Error'),
		('CP','Critical Problem'),
		('DP','Documentation Problem'),
		('PT','Problem in Testing'),
	)
	
	PRIOR_CHOICES = (
		('Low','Low'),
		('Nml','Normal'),
		('Hig','High'),
	)
	
	STATUS_CHOICES = (
		('NC','Not Confirmed'),
		('CO','Confirmed'),
		('Fix','Fixed'),
		('STF','Start Fixing'),
		('Rej','Rejected'),
		('FCO','Fix Confirmed'),
		('3dp','3rd party'),
	)
	project = models.OneToOneField('Project')
	version = models.OneToOneField('Versions')
	
	title = models.TextField()
	kind = models.CharField(max_length=3,choices=KIND_CHOICES)
	priority = models.CharField(max_length=3, choices=PRIOR_CHOICES)
	status = models.CharField(max_length=3, choices=STATUS_CHOICES)
	statusDate = models.DateField()
	assigned = models.OneToOneField('User',related_name='assigned')
	author = models.OneToOneField('User',related_name='author')
	description = models.TextField()
	fixDescription = models.TextField()

	def __unicode__(self):
		return self.name
	
	
class BugHistory(models.Model):
	ACTION_CHOICES = (
		('Cre','Create'),
		('Edt','Edit'),
		('CO','Confirm'),
		('STF','Start fixing'),
		('Tak','Take'),
		('Agn','Assign'),
		('Cmt','Comment'),
		('Rei','Re-instate'),
		('Cof','Confirm fixing'),
		('Fix','Fixed'),
	)
	bugid = models.ForeignKey('Bug')
	author = models.OneToOneField('User')
	action = models.CharField(max_length=3,choices=ACTION_CHOICES)
	date = models.DateTimeField(auto_now_add=True)
	information = models.TextField()
	
	def __unicode__(self):
		return self.name


class BugAttachedFiles(models.Model):
	bugid = models.ForeignKey('Bug',related_name='bug')
	file = models.FileField(upload_to='bugs')

	def __unicode__(self):
		return self.name
	
class Task(models.Model):
	PRIOR_CHOICES = (
		('Low','Low'),
		('Nml','Normal'),
		('Hig','High'),
	)
	project = models.ForeignKey('Project')
	name = models.TextField()
	priority = models.CharField(max_length=3,choices=PRIOR_CHOICES)
	kind = models.CharField(max_length=30)
	request = models.CharField(max_length=30)
	assigment = models.ManyToManyField('User')
	createDate = models.DateTimeField()
	startDate = models.DateTimeField()
	finishDate = models.DateTimeField()
	closeDate = models.DateTimeField()
	workPlan = models.TextField()
	description = models.TextField()

	def __unicode__(self):
		return self.name
	
class TaskHistory(models.Model):
	task = models.ManyToManyField('Task')
	createDate = models.DateTimeField(auto_now_add=True)
	text = models.TextField()

	def __unicode__(self):
		return self.name
	
class TaskNote(models.Model):
	task = models.ManyToManyField('Task')
	createDate = models.DateTimeField(auto_now=True)
	text = models.TextField()
	lastAuthor = models.OneToOneField('User')

	def __unicode__(self):
		return self.name
	

class Request(models.Model):
	PRIOR_CHOICES = (
		('Low','Low'),
		('Nml','Normal'),
		('Hig','High'),
	)
	project = models.ForeignKey('Project')
	name = models.TextField()
	priority = models.CharField(max_length=3,choices=PRIOR_CHOICES)
	source = models.CharField(max_length=30)
	createDate = models.DateTimeField(auto_now_add=True)
	description = models.TextField()

	def __unicode__(self):
		return self.name
	
class RequestNote(models.Model):
	request = models.ManyToManyField('Request')
	createDate = models.DateTimeField(auto_now=True)
	text = models.TextField()
	lastAuthor = models.OneToOneField('User')

	def __unicode__(self):
		return self.name

class TestCase(models.Model):
	name = models.CharField(max_length=100)
	instruction = models.TextField()
	author = models.OneToOneField('User')
	createDate = models.DateTimeField(auto_now_add=True)

	def __unicode__(self):
		return self.name
	
class TestCaseNote(models.Model):
	testCase = models.ManyToManyField('TestCase')
	createDate = models.DateTimeField(auto_now=True)
	text = models.TextField()
	lastAuthor = models.OneToOneField('User')

	def __unicode__(self):
		return self.testcase

class Release(models.Model):
	project = models.OneToOneField('Project')
	name = models.CharField(max_length=100)
	version = models.OneToOneField('Versions',related_name='release_version')
	devRelease = models.DateTimeField()
	qaRelease = models.DateTimeField()
	startTesting = models.DateTimeField()
	endTesting = models.DateTimeField()
	custRelease = models.DateTimeField()
	expectedEnvironment = models.TextField()
	installNotes = models.TextField()
	deployNotes = models.TextField()
	issuesBugs = models.ManyToManyField('Bug')
	issuesTasks = models.ManyToManyField('Task')
	issuesRequests = models.ManyToManyField('Request')

	def __unicode__(self):
		return self.name

class ReleaseHistory(models.Model):
	release = models.OneToOneField('Release')
	createDate = models.DateTimeField(auto_now_add=True)
	text = models.TextField()

	def __unicode__(self):
		return self.release

class Versions(models.Model):
	project = models.OneToOneField('Project')
	version = models.CharField(max_length=20)
	desc = models.TextField()
	release = models.DateField()
	
	def __unicode__(self):
		return self.version
